package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.model.common.dtos.admin.ChannelDto;
import com.heima.model.common.dtos.common.PageDto;
import com.heima.model.common.dtos.common.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 自媒体端频道相关接口
 */
@Api(value = "/api/v1/channel", tags = {"自媒体端频道相关接口"})
@RestController
@RequestMapping("/api/v1/channel")
public class WmChannelController {
    @Autowired
    private WmChannelService wmChannelService;

    /**
     * 频道列表获取
     * @return
     */
    @ApiOperation(value = "频道列表获取", notes = "频道列表获取", httpMethod = "GET")
    @GetMapping("/channels")
    public ResponseResult channels() {
        List<WmChannel> list = wmChannelService.list();
        return ResponseResult.okResult(list);
    }

    /**
     * 分页获取频道列表
     * @param pageDto
     * @return
     */
    @PostMapping("/list")
    public ResponseResult list(@RequestBody PageDto pageDto) {
        return wmChannelService.pageList(pageDto);
    }

    /**
     * 保存频道信息
     * @param channelDto
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChannelDto channelDto) {
        return wmChannelService.saveChannel(channelDto);
    }
    @PostMapping("/update")
    public ResponseResult update(@RequestBody ChannelDto channelDto) {
        //检验参数
        if(channelDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaUpdateWrapper<WmChannel> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmChannel::getId, channelDto.getId());
        if(channelDto.getOrd() != null){
            wrapper.set(WmChannel::getOrd, channelDto.getOrd());
        }
        if(channelDto.getName() != null){
            wrapper.set(WmChannel::getName, channelDto.getName());
        }
        if(channelDto.getDescription() != null){
            wrapper.set(WmChannel::getDescription, channelDto.getDescription());
        }
        if(channelDto.getStatus() != null){
            wrapper.set(WmChannel::getStatus, channelDto.getStatus());
        }
        wmChannelService.update(wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    @GetMapping("/del/{channelId}")
    public ResponseResult del(@PathVariable("channelId") Integer channelId) {
        //检验参数
        if(channelId == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //查询表中id为channelId的频道信息，判断状态
        WmChannel wmChannel = wmChannelService.getById(channelId);
        //只有状态为禁用才可删除
        if(wmChannel.getStatus() == 1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"状态为禁用才可以删除");
        }
        wmChannelService.removeById(channelId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
